/*
 *  Copyright 2010 BeTo.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package edu.upf.vod;

import edu.upf.core.ConfigDiscover;
import edu.upf.utils.LogSystem;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author BeTo
 */
public class VoDServer {

    private String command, confpath, vlcRoute;
    private Process send;
    private Thread input, output;
    private boolean isWindows = false;
    private LogSystem log;

    /**
     * When we start the server, define the path of the videoserver.vlm file, needed to
     * load the needed configuration for VLC VoD service
     * @throws IOException
     */
    public VoDServer() throws IOException {
        init();
        command = vlcRoute + " --ttl=12 -vvv --color -I telnet --telnet-password=videolan --rtsp-host=0.0.0.0:5554 --vlm-conf=" + confpath;
        log = new LogSystem(this.getClass());
    }

    public void init() throws IOException{
        ConfigDiscover conf = ConfigDiscover.getInstance();
        conf.setConfPath();
        vlcRoute=conf.getVLCRoute();
        confpath=conf.getConfPath();
    }

    public void close() {
        input.interrupt();
        output.interrupt();
        send.destroy();
    }

    public void start() {
        try {
            /*We execute a new process with the command needed to run up the
            server*/
            send = Runtime.getRuntime().exec(command);

            /*At this point we start two different Threads to control the
            Process we have created before, catching the errors and other
            information of the input*/
            input = new Thread() {
                @Override
                public void run() {
                    try {
                        InputStream is = send.getInputStream();
                        byte[] buffer = new byte[1024];
                        for (int count = 0; (count = is.read(buffer)) >= 0;) {
                            System.out.write(buffer, 0, count);
                        }
                    } catch (IOException ex) {
                        log.addInfoLog("VLC Process killed: "+ex.getMessage());
                    }
                }
            };
            input.start();

            output = new Thread() {
                @Override
                public void run() {
                    try {
                        InputStream is = send.getErrorStream();
                        byte[] buffer = new byte[1024];
                        for (int count = 0; (count = is.read(buffer)) >= 0;) {
                            System.err.write(buffer, 0, count);
                        }
                    } catch (IOException e) {
                        log.addInfoLog("VLC Process killed");
                    }
                }
            };
            output.start();
        } catch (IOException ex) {
            log.addInfoLog(ex.getMessage());
        }
    }
}

